package com.bdqn.daoimpl;

import java.util.List;

import com.bdqn.dao.BaseDao;
import com.bdqn.dao.MsgDao;
import com.bdqn.entity.Msg;
import com.bdqn.entity.Msguserinfo;

public class MsgDaoImpl  extends  BaseDao implements MsgDao {

	@Override
	public List<Msguserinfo> getToUsers(Msguserinfo user) {
		String  sql="select  *  from  Msguserinfo  where username!=? ";
		return super.selectList(sql, new Object[]{user.getUsername()}, Msguserinfo.class);
	}

	@Override
	public int sendMsg(Msg msg) {
		String  sql="INSERT INTO `msg` ( `username`, `title`, `msgcontent`, `sendto`) VALUES (?, ?, ?, ?)";
		return super.executeUpdate(sql, new Object[]{msg.getUsername(),msg.getTitle(),msg.getMsgcontent(),msg.getSendto()});
	}

	@Override
	public int countUserMsg(Msguserinfo user) {
		String sql="select  count(0)  from  msg  where sendto  =?";
		Long  num=(Long) super.selectOne(sql, new Object[]{user.getUsername()});
		return Integer.valueOf(num.toString());
	}

	@Override
	public List<Msg> getPageMsg(Integer start, Integer row, Msguserinfo user) {
		String sql="select  * from  msg  where sendto  =?  limit ?,?";
		return super.selectList(sql, new Object[]{user.getUsername(),start,row},Msg.class);
	}

	@Override
	public Msg readmsg(Integer id) {
		return super.selectOne("select  * from  msg  where msgid=?", new Object[]{id}, Msg.class);
	}
	@Override
	public int deleteMsg(Integer id) {
		/*String  sql="delete  from   Msg  where msgid=? ";
		return super.executeUpdate(sql, new Object[]{id});*/
		return  super.deleteById(Msg.class, id);
	}

	@Override
	public int updateState(Integer id, Integer stateType) {
		String  sql="update  Msg  set  state =? where msgid=? ";
		return super.executeUpdate(sql, new Object[]{stateType,id});
	}

}
